package gui;

import entity.User;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.border.SoftBevelBorder;

import util.MyFactory;

public class MenuFrame extends BaseFrame {
	private String name = "XXX";
	private final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
	private final int WEIGHT = Toolkit.getDefaultToolkit().getScreenSize().width;

	public MenuFrame() {
		init();
	}

	public MenuFrame(ClientContext context) {
		this();
		this.context = context;
	}

	@Override
	protected void init() {
		this.setLayout(null);
		setTitle("学生在线考试系统");
		setSize(600, 400);
		setLocation((WEIGHT - 600) / 2, (HEIGHT - 400) / 2);
		String lookAndFeel = null;
		lookAndFeel = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (Exception e) {

		}
		createButtons();
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				context.menuExit();
			}
		});
	}
	/**添加按钮*/
	private void createButtons() {
		JButton start = getButton("开始", "exam.png");
		start.setBounds(100, 125, 70, 90);
		start.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.start();
			}
		});
		JButton result = getButton("分数", "result.png");
		result.setBounds(200, 125, 70, 90);
		result.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.result();
			}
		});
		JButton message = getButton("规则", "message.png");
		message.setBounds(300, 125, 70, 90);
		message.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.message();
			}
		});
		JButton exit = getButton("离开", "exit.png");
		exit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.menuExit();
			}
		});
		exit.setBounds(400, 125, 70, 90);
	}

	public void setName(String name) {
		if (this.name == null) {
			this.name = "XXX";
			return;
		}
		this.name = name;
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		ImageIcon back = MyFactory.getImageIcon("exam-back.png");
		JLabel jl = new JLabel(back);
		jl.setBounds(0, 0, 600, 400);
		this.add(jl);
		g.setFont(new Font("隶书", Font.BOLD, 20));
		g.drawString("欢迎： " + name + "同学！", 240, 140);
		g.drawString("作者：宋利军", 380, 370);
		ImageIcon icon = MyFactory.getImageIcon("title.png");
		g.drawImage(icon.getImage(), 30, 40, 540, 83, this);
	}
	/**一个创建按钮的工厂方法*/
	public JButton getButton(String name, String image) {
		JButton jb = new JButton(MyFactory.getImageIcon(image));
		jb.setText(name);
		jb.setBorder(new SoftBevelBorder(0));
		jb.setVerticalTextPosition(JButton.BOTTOM);
		jb.setHorizontalTextPosition(JButton.CENTER);
		this.add(jb);
		return jb;
	}
	/**更新窗体*/
	public void upadateView() {
		User user = context.getUser();
		name = user.getName();
		System.out.println("name=" + name);
	}
}
